home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Oh!X 2000 Spring
/
Oh!X 2000 Spring Special CD-ROM (Japan) (Part 1).7z
/
Oh!X 2000 Spring Special CD-ROM (Japan) (Part 1).bin
/
MIPSSIM
/
mem.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-12-02
|
3KB
|
162 lines
#include <stdio.h>
#include "define.h"
/*
Memory Module
*/
static char mem[MAXPAGENUM][0x10000];
static ulong pair[MAXPAGENUM];
static int pair_ptr= -1;
static int search(ulong addr)
{
int i;
if(pair_ptr<0){ /* init */
pair_ptr = 0;
pair[pair_ptr] = addr&0xffff0000L;
return pair_ptr;
}
for(i=0;i<=pair_ptr;i++){
if(((pair[i]^addr)&0xffff0000L)==0)
break;
}
if(i<=pair_ptr){ /* match */
return i;
}
if((pair_ptr+1)<MAXPAGENUM){ /* allocate */
pair_ptr++;
pair[pair_ptr] = addr&0xffff0000L;
return pair_ptr;
}
printf("mem.c:out of memory at 0x%08x\n",addr);
return -1;
}
ulong get_pword(ulong addr)
{
int i;
int c0,c1,c2,c3;
addr &= 0xfffffffcL;
i = search(addr);
if(i>=0){
addr &= 0xffff;
c0 = mem[i][addr+0]&0xff;
c1 = mem[i][addr+1]&0xff;
c2 = mem[i][addr+2]&0xff;
c3 = mem[i][addr+3]&0xff;
return (c3<<24)|(c2<<16)|(c1<<8)|(c0);
}
return 0;
}
void put_pword(ulong addr, ulong data)
{
int i;
int c0,c1,c2,c3;
addr &= 0xfffffffcL;
i = search(addr);
if(i>=0){
addr &= 0xffff;
c0 = (data )&0xff;
c1 = (data>> 8)&0xff;
c2 = (data>>16)&0xff;
c3 = (data>>24)&0xff;
mem[i][addr+0] = c0;
mem[i][addr+1] = c1;
mem[i][addr+2] = c2;
mem[i][addr+3] = c3;
}
}
ulong get_word(ulong addr)
{
return get_pword(addr&0x1ffffffcL);
}
void put_word(ulong addr, ulong data)
{
put_pword(addr&0x1ffffffcL, data);
}
void mem_dump(ulong start, ulong end, int unit)
{
ulong m_start;
ulong m_end;
ulong a,c;
int width,ainc;
int data;
unsigned char asc[16];
unsigned char d0,d1,d2,d3;
if(unit==0){
width=16;
ainc =1;
}
else if(unit==1){
width=8;
ainc =2;
}
else {
unit =2;
width=4;
ainc =4;
}
m_start = start&0xfffffffcL;
m_end = end &0xfffffffcL;
for(a=m_start,c=0;a<m_end;a += 4){
if(c==0) printf("%08x: ",a);
data=get_pword(a);
asc[4*c+0]=d0=data&0xff;
asc[4*c+1]=d1=(data>>8)&0xff;
asc[4*c+2]=d2=(data>>16)&0xff;
asc[4*c+3]=d3=(data>>24)&0xff;
switch(unit){
case 0:
printf("%02x %02x %02x %02x ",d0,d1,d2,d3);
break;
case 1:
printf("%04x %04x ",(d1<<8)|d0,(d3<<8)|d2);
break;
case 2:
printf("%08x ",data);
break;
}
asc[4*c+0]=((d0>=' ')&&(d0<='~')) ? d0 : '.';
asc[4*c+1]=((d1>=' ')&&(d1<='~')) ? d1 : '.';
asc[4*c+2]=((d2>=' ')&&(d2<='~')) ? d2 : '.';
asc[4*c+3]=((d3>=' ')&&(d3<='~')) ? d3 : '.';
c++;
if(c==4){
int i;
c=0;
for(i=0;i<16;i++) printf("%c",asc[i]);
printf("\n");
}
}
if(c!=0){
int i;
while(c<4){
switch(unit){
case 0:
printf(" ",d0,d1,d2,d3);
break;
case 1:
printf(" ",(d1<<8)|d0,(d3<<8)|d2);
break;
case 2:
printf(" ",data);
break;
}
asc[4*c+0]=' ';
asc[4*c+1]=' ';
asc[4*c+2]=' ';
asc[4*c+3]=' ';
c++;
}
for(i=0;i<16;i++) printf("%c",asc[i]);
printf("\n");
}
}